package za.org.jcicada.cfgmngr.impl;

import java.net.URI;
import java.util.Set;

import za.org.jcicada.cfgmngr.IConfigurationClient;
import za.org.jcicada.cfgmngr.IConfigurationServer;
import za.org.jcicada.cfgmngr.SystemSetType;
import za.org.jcicada.cfgmngr.messages.DeregisterServiceReply;
import za.org.jcicada.cfgmngr.messages.DeregisterSystemReply;
import za.org.jcicada.cfgmngr.messages.GetConfigurationReply;
import za.org.jcicada.cfgmngr.messages.LocateServiceReply;
import za.org.jcicada.cfgmngr.messages.RegisterServiceReply;
import za.org.jcicada.cfgmngr.messages.RegisterSystemReply;
import za.org.jcicada.cfgmngr.messages.SensorSetReply;
import za.org.jcicada.cfgmngr.messages.ServiceSetReply;
import za.org.jcicada.cfgmngr.messages.SystemSetReply;
import za.org.jcicada.comms.IConnection;
import za.org.jcicada.comms.impl.MessagingBase;
import za.org.jcicada.genss.ServiceURN;
import za.org.jcicada.genss.SystemURN;
import za.org.jcicada.genss.messages.Result;
import za.org.jcicada.genss.sensor.SensorURN;

public class ConfigurationMessaging extends MessagingBase {

    /**
     * Handle to configuration server for invoking methods on.
     */
    IConfigurationServer configurationServer;

    /**
     * Handle to callback object for messages from the subject to the observer.
     */
    IConfigurationClient client;

    public ConfigurationMessaging(IConnection connection, IConfigurationServer configurationServer) {
        super(connection);
        this.configurationServer = configurationServer;
        client = new Responder();
    }

    public void onClose() throws Exception {
        configurationServer.informDisconnect(client);
    }
    
    public IConfigurationClient getClient() {
    	return client;
    }

    /**
     * Dispatch DeregisterServicesRequest.
     */
    public void cfgmngrDeregisterServiceRequest(za.org.jcicada.cfgmngr.messages.DeregisterServiceRequest msg) {
        configurationServer.requestDeregisterService(client, msg.getLocationURI(), msg.getServiceURN());
    }

    /**
     * Dispatch GetConfigurationRequest.
     */
    public void cfgmngrGetConfigurationRequest(za.org.jcicada.cfgmngr.messages.GetConfigurationRequest msg) {
        configurationServer.requestGetConfiguration(client, msg.getSubsystemURN());
    }

    /**
     * Dispatch LocateServiceRequest.
     */
    public void cfgmngrLocateServiceRequest(za.org.jcicada.cfgmngr.messages.LocateServiceRequest msg) {
        configurationServer.requestLocateService(client, msg.getServiceURN(), msg.getProtocol());
    }

    /**
     * Dispatch RegisterServicesRequest.
     */
    public void cfgmngrRegisterServiceRequest(za.org.jcicada.cfgmngr.messages.RegisterServiceRequest msg) {
        configurationServer.requestRegisterService(client, msg.getLocationURI(), msg.getServiceURN());
    }

    public void cfgmngrDeregisterSystemRequest(za.org.jcicada.cfgmngr.messages.DeregisterSystemRequest msg) {
        configurationServer.requestDeregisterSystem(client, msg.getSystemURI());
    }

    public void cfgmngrRegisterSystemRequest(za.org.jcicada.cfgmngr.messages.RegisterSystemRequest msg) {
        configurationServer.requestRegisterSystem(client, msg.getSystemURN());
    }

    public void cfgmngrSystemSetRequest(za.org.jcicada.cfgmngr.messages.SystemSetRequest msg) {
        configurationServer.requestSystemSet(client, msg.getType());
    }

    public void cfgmngrSensorSetRequest(za.org.jcicada.cfgmngr.messages.SensorSetRequest msg) {
        configurationServer.requestSensorSet(client, msg.getSystemURN());
    }
    
    public void cfgmngrServiceSetRequest(za.org.jcicada.cfgmngr.messages.ServiceSetRequest msg) {
    	configurationServer.requestServiceSet(client, msg.getSystemURN());
    }

    /**
     * Implementation of IConfigurationClient that sends messages back to a
     * katcomms connection.
     */
    public class Responder extends MessagingBase.ResponderBase implements IConfigurationClient {

        public void replyDeregisterService(Result result, URI locationURI, ServiceURN serviceURN) {
            sendMessage(new DeregisterServiceReply(result, locationURI, serviceURN));
        }

        public void replyGetConfiguration(Result result, Object configuration) {
            sendMessage(new GetConfigurationReply(result, configuration));
        }

        public void replyLocateService(Result result, ServiceURN serviceURN, URI locationURI) {
            sendMessage(new LocateServiceReply(result, serviceURN, locationURI));
        }

        public void replyRegisterService(Result result, URI locationURI, ServiceURN serviceURN) {
            sendMessage(new RegisterServiceReply(result, locationURI, serviceURN));
        }

        public void replyDeregisterSystem(Result result, SystemURN systemURN) {
            sendMessage(new DeregisterSystemReply(result, systemURN));
        }

        public void replyRegisterSystem(Result result, SystemURN systemURN, boolean reregister) {
            sendMessage(new RegisterSystemReply(result, systemURN, reregister));
        }

        public void replySystemSet(Result result, SystemSetType type, Set<SystemURN> systemURNs) {
            sendMessage(new SystemSetReply(result, type, systemURNs));
        }

        public void replySensorSet(Result result, SystemURN systemURN, Set<SensorURN> sensorURNs) {
            sendMessage(new SensorSetReply(result, systemURN, sensorURNs));
        }
        
        public void replyServiceSet(Result result, SystemURN systemURN, Set<ServiceURN> serviceURNs) {
        	sendMessage(new ServiceSetReply(result, systemURN, serviceURNs));
        }

    }
}
